package com.example.sunlight.interneteverything;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.arcsoft.sdk_demo.DetecterActivity;
import com.example.sunlight.interneteverything.View.BtnView;
import com.example.sunlight.interneteverything.View.Views;
import com.example.wzsdk.login.Landing;
import com.example.wzsdk.login.LoginState;
import com.example.wzsdk.scan.Search;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import data.Datas;
import server.MyAlerDialog;

public class EverythingMain extends Activity implements BtnView.OnClicking, View.OnClickListener, MyAlerDialog.OnEditInputFinishedListener, Search.SearchFish, LoginState {
    RelativeLayout parent;
    Views views;
    ImageView imageView;
    BtnView environment, health, agriculture, traffic, machine, storage;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    search.EngSearch(EverythingMain.this);
                    break;
                case 0x456:
                    state_str.setText("与主机断开连接!!");
//                    dialog();
                    break;
                case 0x789:
                    if (!sp.getString("ip", "null").equals("null")) {
                        landing.Login(sp.getString("ip", "null"), 5800);
                    } else {
                        Toast.makeText(EverythingMain.this, "请配置主机信息", Toast.LENGTH_LONG).show();
                    }
                    break;
                case 0x666:
                    state_str.setText(sn + "");
                    break;
            }
        }
    };


    protected void dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(EverythingMain.this);
        builder.setCancelable(false);
        builder.setMessage("与主机断开连接！");
        builder.setTitle("系统提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                landing = new Landing("QWDZSCDAQ");
//                landing.setLoginState(EverythingMain.this);
//                landing.Login(sp.getString("ip", "null"), 5800);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = getSharedPreferences("text", Context.MODE_PRIVATE);
        FindView();
        OnClick();
        views = new Views(this);
        parent.addView(views);
        list = new ArrayList<>();
        myAlerDialog = new MyAlerDialog(this, list, this);
        initSDK();
//        handler.sendEmptyMessageDelayed(0x789, 1000);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public void initSDK() {
        search = new Search("QWDZSCDAQ");
        search.setSearchFish(this);
        landing = new Landing("QWDZSCDAQ");
        landing.setLoginState(this);
    }

    MyAlerDialog myAlerDialog;
    List<String[]> list;
    Search search;
    Landing landing;
    EditText ip, host;
    TextView state_str;

    public void ShowDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.dialog);
        View view = View.inflate(this, R.layout.alertdialog_item, null);
        builder.setView(view);
        builder.setCancelable(true);
        ip = (EditText) view.findViewById(R.id.ip);
        host = (EditText) view.findViewById(R.id.host);
        Button login = (Button) view.findViewById(R.id.login);
        Button scan = (Button) view.findViewById(R.id.search);

        final AlertDialog dialog = builder.create();
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0x123);
                myAlerDialog.show();
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp.edit().putString("ip", ip.getText().toString()).commit();
                landing = new Landing("QWDZSCDAQ");
                landing.setLoginState(EverythingMain.this);
                landing.Login(ip.getText().toString(), 5800);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public void FindView() {
        parent = (RelativeLayout) findViewById(R.id.parent);
        environment = (BtnView) findViewById(R.id.environment);
        health = (BtnView) findViewById(R.id.health);
        agriculture = (BtnView) findViewById(R.id.agriculture);
        traffic = (BtnView) findViewById(R.id.traffic);
        machine = (BtnView) findViewById(R.id.machine);
        storage = (BtnView) findViewById(R.id.storage);
        imageView = (ImageView) findViewById(R.id.imageView);
        state_str = (TextView) findViewById(R.id.state);
    }

    public void OnClick() {
        environment.setOnClicking(this);
        health.setOnClicking(this);
        agriculture.setOnClicking(this);
        traffic.setOnClicking(this);
        machine.setOnClicking(this);
        storage.setOnClicking(this);
        imageView.setOnClickListener(this);
    }


    @Override
    public void OnClik(String text) {
        switch (text) {
            case "环境感知":
                Intent env = new Intent(this, EnvironmentAwareness.class);
                startActivity(env);
                break;
            case "健康管理":
                Intent hea = new Intent(this, HealthManagement.class);
                startActivity(hea);
                break;
            case "新农业":
                Intent news = new Intent(this, NewAgriculture.class);
                startActivity(news);
                break;
            case "交通调度":
                Intent tsc = new Intent(this, TrafficScheduling.class);
                startActivity(tsc);
                break;
            case "安全识别":
                Intent tscs = new Intent(this, DetecterActivity.class);
                startActivity(tscs);
                break;
            case "智慧仓储":
                Intent ins = new Intent(this, IntelligentStorage.class);
                startActivity(ins);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        landing.exitLogin();
    }

    int state = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView:
                if (state == 5) {
                    ShowDialog();
                    state = 0;
                } else {
                    ++state;
                }
                break;
        }
    }

    @Override
    public void editInputFinished(String ips, String serials, int number) {
        ip.setText(ips);
        host.setText(serials);
    }

    @Override
    public void SearchFishWifi(List<String[]> list) {

    }

    @Override
    public void SearchFishWna(List<String[]> list) {
        if (list != null) {
            myAlerDialog.setData(list);
            myAlerDialog.refurbish();
        }
    }

    String sn = "";

    @Override
    public void LoginStatus(boolean b, String s) {
        if (b == true) {
            landing.atnDevice(Datas.Rfid);
            landing.atnVariable(Datas.Uiid);
            sn = s;
            handler.sendEmptyMessage(0x666);
        } else {
            if (!sp.getString("ip", "null").equals("null")) {
                handler.sendEmptyMessage(0x456);
            }
        }

    }

    @Override
    public void AttentionStarting(String uuid, byte number, String index, String porid, String host) {
        switch (uuid) {
            case Datas.Uiid:
                if (index.equals("1")) {
                    Datas.Tem = number + "℃";
                } else {
                    Datas.Hum = number + "%Rh";
                    Datas.Co2 = new Random().nextInt(40) + "%";
                    Datas.Jia = "0." + new Random().nextInt(5) + "mg/m3";
                }
                break;
        }
    }

    @Override
    public void AttentionProcess(String uuid, byte number, String text, String index, String proid, String host) {
        System.out.println("cqwonvrulz   " + uuid + "   " + text);
        switch (uuid) {
            case Datas.Uiid:
                if (index.equals("1")) {
                    Datas.Tem = number + "℃";
                } else {
                    Datas.Hum = number + "%Rh";
                    Datas.Co2 = new Random().nextInt(40) + "%";
                    Datas.Jia = "0." + new Random().nextInt(4) + 1 + "mg/m3";
                }
                break;
            case "30013":
                Intent intent = new Intent("com.brock.rfid");
                intent.putExtra("text", text);
                sendBroadcast(intent);
                break;
        }
    }

    @Override
    public void SetVarState(String s, byte b, String s1) {

    }

    @Override
    public void SetKeyState(String s, String s1, String s2) {

    }
}
